<jsp:include page="/templates/header.jsp"/>
<%@page import="controller.*"%>
<%@page import="database.Queries"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.sql.Connection" %>
<%@page import="database.Connect" %>
<%@page import="java.sql.SQLException"%>

<script type="text/javascript" src="/salonmorandsmigi/js/jquery.editable-1.3.3.js"></script>
<script type="text/javascript" src="/salonmorandsmigi/js/sinfo.js"></script>

 <h1>Hello <%=request.getUserPrincipal().getName()%>, here is your data:</h1>
 <div class="mycourses">
 <h3 class="mycourses"><strong>Note: </strong>you can edit any field in your personal data, except your user name</h3>
 </div>
 <div class="instruction">click on a field content to change it. click 'save' to save the change or 'cancel' to leave it as it was.
 </div>
 <%
 	Connection con=null;
 	try
 	{
	 	con=Connect.getConnection();
	 	con.setAutoCommit(false);
 		Student s = Queries.createStudent(con,request.getUserPrincipal().getName());
 		con.commit();
	 	if (s==null){
	 		%><h1>No such student exists</h1>
	 	<% }
		else { %>	
		 <div class="details">
				 <p class="username"><strong>username:</strong>&nbsp;&nbsp;<a class="editable1"><%=s.getUsername()%></a></p>
			  	 <p><strong>password:</strong>&nbsp;&nbsp;<a class="editable2"><%=s.getPassword()%></a></p>  
			  	 <p><strong>full name:</strong>&nbsp;&nbsp;<a class="editable3"><%=s.getFullname()%></a></p> 
			  	 <p><strong>phone number:</strong>&nbsp;&nbsp;<a class="editable4"><%=s.getPhone()%></a></p>
		 </div>
		 <%
		 	if (request.isUserInRole("student")){
			  	LinkedList<Course> courses = s.getCourses();
			    if (courses==null){%>
			    	<div class="rediv">
			    		<h2>The student isn't registered to any course</h2>
			    	  </div>
			    <%}
			    else {%> 
		    		<div class="mycourses">
		    		<a href="/salonmorandsmigi/student/mycourses.jsp">My Courses</a>
		    		</div>
			    <% }
		 	}
		 	else {%>
		 		<div class="rediv">
			    <h2>You are an Admin. you can't register to courses</h2>
			    </div>
		 	<%}
		 }
 	}
 	 catch (SQLException e){
 		con.rollback();
 		out.write("SERVER ERROR! PLEASE TRY AGAIN");
 	 }
 	 finally{
 	  	if(con!=null) 
 	  	{
 	  		 con.setAutoCommit(true);
 	  		 con.close();
 	  	}
 	 }
		 %>
    
<jsp:include page="/templates/sidebar.jsp"/>
<jsp:include page="/templates/footer.jsp"/>